/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.coinche.client.ui.card.impl;

import org.jb.coinche.client.business.model.Auction;
import org.jb.coinche.client.business.model.Trump;
import org.jb.coinche.client.ui.card.AuctionPanel;
import org.jb.coinche.client.ui.card.AuctionPanel.AuctionListener;
import org.jb.coinche.client.ui.rsc.CardColorResource;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.extjs.gxt.ui.client.widget.toolbar.LabelToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.SeparatorToolItem;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/**
 * 
 * @author jeremie
 *
 */
public class AuctionToolbar extends ToolBar implements AuctionPanel, AuctionListener {


	public AuctionToolbar() {
		
	}
	
	public void refresh() {
		removeAll();
		
		LabelToolItem label = new LabelToolItem();
		label.setWidth( 100 );
		if (auction != null) {
			String coincheMode = "";
			switch (auction.getCoincheMode()) {
			case COINCHE:
				coincheMode = " <span style=\"position: relative; top: -3\">Coinché</span>";
				break;
			case SURCOINCHE:
				coincheMode = " <span style=\"position: relative; top: -3\">Surcoinché</span>";
				break;
			}
			label.setLabel( "<div style=\"position:relative\"><span style=\"position: relative; top: -3\"> " + auction.getPlayer().getName() + ": " + Integer.toString( auction.getValue() ) + " </span>" + AbstractImagePrototype.create(getColorImage(auction.getTrump())).getHTML() + coincheMode +  "</div>" );
		} else {
			label.setLabel( "Pas d'enchère" );
		}
		add( label );
		
		if (editable) {
			add( new SeparatorToolItem() );
			for( final Trump color : Trump.values() ) {
				
				Button btn = new Button();
				btn.setIcon(AbstractImagePrototype.create(getColorImage(color)));
				btn.setToolTip( color.toText() );
				
				int min = auction == null ? 80 : auction.getValue() + 10;
				if (min >= 160) {
					btn.disable();
				} else {
					Menu menu = new Menu();
					
					for( int i = min; i <= 160; i += 10 ) {
						final int value = i;
						MenuItem item = new MenuItem( i == 160 ? "Capot" : Integer.toString(i) );
						item.addSelectionListener( new SelectionListener<MenuEvent>() {
							@Override
							public void componentSelected(MenuEvent ce) {
								onAuction(color, value);	
							}
						});
						menu.add( item );
					}
					
					btn.setMenu(menu);
				}
				
				add( btn );
			}
			add( new SeparatorToolItem() );
			Button btnPasse = new Button("Passer");
			btnPasse.addSelectionListener( new SelectionListener<ButtonEvent>() {
				@Override
				public void componentSelected(ButtonEvent ce) {
					onPass();
				}
			});
			add( btnPasse );
		}
		if (coincheEnabled) {
			if (!editable) {
				add( new SeparatorToolItem() );
			}
			Button btnCoinche = new Button("Coincher");
			btnCoinche.addSelectionListener( new SelectionListener<ButtonEvent>() {
				@Override
				public void componentSelected(ButtonEvent ce) {
					onCoinche();
				}
			});
			add( btnCoinche );
		}

		layout();
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.AuctionPanel#setAuctionListener(org.jb.coinche.client.gui.card.AuctionPanel.AuctionListener)
	 */
	@Override
	public void setAuctionListener(AuctionListener listener) {
		this.listener = listener;
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.AuctionPanel#setCoincheEnabled(boolean)
	 */
	@Override
	public void setCoincheEnabled(boolean coincheEnabled) {
		this.coincheEnabled = coincheEnabled;	
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.AuctionPanel#setCurrentAuction(org.jb.coinche.client.business.model.Auction)
	 */
	@Override
	public void setCurrentAuction(Auction auction) {
		this.auction = auction;
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.AuctionPanel#setEditable(boolean)
	 */
	@Override
	public void setEditable(boolean editable) {
		this.editable = editable;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.AuctionPanel.AuctionListener#onAuction(org.jb.coinche.client.business.model.Card.Color, int)
	 */
	@Override
	public void onAuction(Trump trump, int value) {
		if (listener != null)
			listener.onAuction(trump, value);
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.AuctionPanel.AuctionListener#onCoinche()
	 */
	@Override
	public void onCoinche() {
		if (listener != null)
			listener.onCoinche();
		
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.gui.card.AuctionPanel.AuctionListener#onPass()
	 */
	@Override
	public void onPass() {
		if (listener != null)
			listener.onPass();	
	}
	
	/*
	 * 
	 */
	private ImageResource getColorImage( Trump color ) {
		switch( color ) {
		case CLUBS: return rsc.club();
		case DIAMONDS: return rsc.diamond();
		case HEARTS: return rsc.heart();
		case SPADES: return rsc.spade();
		case TOUTAT: return rsc.toutat();
		case SANSAT: return rsc.sansat();
		default:
			return null;
		}
	}
	
	private CardColorResource rsc = GWT.create(CardColorResource.class);
	private Auction auction = null;
	private boolean editable = false;
	private AuctionListener listener = null;
	private boolean coincheEnabled = false;

	
}
